// 获取该用户的所有文章
async function getArticleListRequest() {
    const user_id = JSON.parse(localStorage.getItem('userInfo')).id
    const res = await $.ajax({
        url: 'http://localhost:8081/api/getArticle.php',
        type: 'get',
        data: {
            user_id
        }
    })
    return res
}

// 渲染文章
async function renderArticleList() {
    const articleListData = await getArticleListRequest()
    const listDom = document.querySelector('.content-main .blog-list')
    listDom.innerHTML = ''
    const fragment = document.createDocumentFragment()
    if (articleListData.code === 1) {
        if (articleListData.total !== 0) {
            articleListData.data.forEach(item => {
                const div = document.createElement('div')
                div.className = 'blog-item'
                div.innerHTML = `
                <div class="left">
                    <p class="title text-ellipsis-1">${item.title}</p>
                    <div class="info">
                        <div class="time">
                            <svg t="1744072146425" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1737" width="256" height="256"><path d="M874.179108 149.919291C777.456788 53.255031 648.909794 0 512.10296 0 375.343491 0 246.779689 53.255031 150.097095 149.929986 53.379358 246.635499 0.118216 375.225275 0.118216 512.003079c-0.024446 136.793083 53.195443 265.353829 149.900956 362.11893 96.670371 96.613839 225.246396 149.878037 361.98753 149.878037 136.806834 0 265.42564-53.297812 362.172407-149.978879C1073.767443 674.41297 1073.798001 349.603883 874.179108 149.919291L874.179108 149.919291 874.179108 149.919291zM826.994433 826.827324c-84.130878 84.118655-195.994709 130.492014-314.986203 130.492014-118.90746 0-230.743788-46.350441-314.819662-130.392701C113.111166 742.833957 66.820314 630.993045 66.838649 512.003079c0-118.97316 46.313771-230.826295 130.420203-314.899113 84.094208-84.074346 195.893867-130.372838 314.844108-130.372838 118.954825 0 230.77893 46.296964 314.891473 130.378949C1000.583984 370.74241 1000.583984 653.25458 826.994433 826.827324L826.994433 826.827324 826.994433 826.827324zM733.350836 637.475934l-176.979966-102.351112L556.37087 256.000011c0-23.557196-19.111001-42.666669-42.674308-42.666669-23.544973 0-42.655973 19.109473-42.655973 42.666669l0 298.669736c0 15.826011 19.671741 29.095841 32.467921 36.437411 3.538621 5.329323 13.268303 10.15291 19.199619 13.564716l176.000581 106.673547c20.388327 11.769431 42.495544 4.774694 54.271086-15.619744 11.775542-20.40055 0.811316-46.469617-19.630487-58.245159l0 0L733.349308 637.475934zM733.350836 637.475934" p-id="1738"></path></svg>
                            <span>${diffForHumans(item.update_at)}</span>
                        </div>
                        <div class="view">
                            <svg t="1744072217355" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3054" width="256" height="256"><path d="M942.2 486.2C847.4 286.5 704.1 186 512 186c-192.2 0-335.4 100.5-430.2 300.3-7.7 16.2-7.7 35.2 0 51.5C176.6 737.5 319.9 838 512 838c192.2 0 335.4-100.5 430.2-300.3 7.7-16.2 7.7-35 0-51.5zM512 766c-161.3 0-279.4-81.8-362.7-254C232.6 339.8 350.7 258 512 258c161.3 0 279.4 81.8 362.7 254C791.5 684.2 673.4 766 512 766z" p-id="3055"></path><path d="M508 336c-97.2 0-176 78.8-176 176s78.8 176 176 176 176-78.8 176-176-78.8-176-176-176z m0 288c-61.9 0-112-50.1-112-112s50.1-112 112-112 112 50.1 112 112-50.1 112-112 112z" p-id="3056"></path></svg>
                            <span>${item.view}</span>
                        </div>
                        <div class="comment">
                            <svg t="1744072260189" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4308" width="256" height="256"><path d="M832 128 192 128C121.344 128 64 185.344 64 256l0 384c0 70.656 57.344 128 128 128l127.808 0 0 65.408c0 24 13.44 45.568 34.944 56.192 8.896 4.288 18.368 6.464 27.776 6.464 13.504 0 26.88-4.416 38.08-12.992L570.048 768 832 768c70.656 0 128-57.344 128-128L960 256C960 185.344 902.656 128 832 128zM896 640c0 35.392-28.608 64-64 64l-272.576-0.064c-7.04 0-13.888 2.304-19.456 6.592l-156.224 122.944 0-97.536c0-17.664-14.336-31.936-31.936-31.936L192 704c-35.392 0-64-28.608-64-64L128 256c0-35.392 28.608-64 64-64l640 0c35.392 0 64 28.608 64 64L896 640zM320 384C284.608 384 256 412.608 256 448s28.608 64 64 64 64-28.608 64-64S355.392 384 320 384zM512 384C476.608 384 448 412.608 448 448s28.608 64 64 64 64-28.608 64-64S547.392 384 512 384zM704 384c-35.392 0-64 28.608-64 64s28.608 64 64 64 64-28.608 64-64S739.392 384 704 384z" p-id="4309"></path></svg>
                            <span>${item.comment_total}</span>
                        </div>
                    </div>
                </div>
                <div data-id="${item.id}" class="right">
                    <button class="btn edit"><a href="../article/articleEdit.php?id=${item.id}">编辑</a></button>
                    <button class="btn delete">删除</button>
                </div>
            `
                fragment.appendChild(div)
            })
            listDom.appendChild(fragment)
        } else {
            document.querySelector('.content-main .blog-list').style.display = 'none'
            document.querySelector('.content-main .empty').style.display = 'block'
        }
    }
}
renderArticleList()

// 删除文章请求
async function deleteArticleRequest(data) {
    const res = await $.ajax({
        url: 'http://localhost:8081/api/articleDelete.php',
        type: 'delete',
        data: data
    })

    return res
}

// 删除文章
document.querySelector('.content-main .blog-list').addEventListener('click', async function (e) {
    if (e.target.classList.contains('delete')) {
        const article_id = e.target.closest('.right').dataset.id
        const res = await deleteArticleRequest({
            article_id
        })
        if (res.code === 1) {
            myAlert(true, '删除成功')
            renderArticleList()
        } else {
            myAlert(false, res.msg)
        }
    }
})